<?php
	class Jabatan_model extends Model{
		function Jabatan_model(){
			parent::Model();
		}
        
        function list_jabatan_struktural($nip="",$where="", $order="", $ro=false) {
              
        	if ($nip=="") {
        		return "";
        	} else {
                $this->db->where(array('nip' => $nip));
                $this->db->or_where(array('nip_baru' => $nip));
        	}
            $this->db->from('pegawai');
            $q = $this->db->get();

            $s = "
              <table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Jabatan</th>
                    <th>Eselon</th>
                    <th>TMT</th>
                    <th>Gaji Pokok</th>
                    <th>Pejabat</th>
                    <th>Nomor SK</th>
                    <th>Tanggal SK</th>
                    ";
            if ($ro==false) {
                $s .= " <th colspan='2'> Aksi </th>";
            }
            $s .= "</tr></thead><tbody>";
			$i = 1;

            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }
                
                //data history jabatan
                $this->db->select('pengangkatan_jabatan.idspg_pengangkatan_jabatan as id, '.
                                  'pengangkatan_jabatan.id_jabatan as id_jabatan, '.
                				  'pengangkatan_jabatan.id_golongan as id_golongan, '.
                				  'pengangkatan_jabatan.gaji_pokok as gaji_pokok, '.
                				  'pengangkatan_jabatan.tmt as tmt, pengangkatan_jabatan.no_sk as no_sk, '.
                                  'pengangkatan_jabatan.tgl_sk as tgl_sk, pengangkatan_jabatan.id_pejabat as id_pejabat, '.
                                  'pengangkatan_jabatan.id_eselon as id_eselon');
                $this->db->from('pengangkatan_jabatan');
                $this->db->join('jabatan', 'jabatan.idspg_jabatan=pengangkatan_jabatan.id_jabatan');
                $this->db->where(array('pengangkatan_jabatan.id_pegawai' => $row->idspg_pegawai));
                $this->db->where(array('jabatan.tipe' => TIPE_JABATAN_STRUKTURAL));
                $this->db->order_by('pengangkatan_jabatan.tmt asc');
                $q1 = $this->db->get();
                if ($q1->num_rows() > 0) {
                	foreach ($q1->result() as $row1) {
		                $pejabat = Datum_model::getData('nama', 'pejabat', array('idspg_pejabat' => $row1->id_pejabat));
		                $jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $row1->id_jabatan));
		                $eselon  = Datum_model::getData('nama', 'eselon', array('idspg_eselon' => $row1->id_eselon));
		                
		                $s .= "
		              <tr class=\"$kelas\" id=\"row-".$row1->id."\" >
		                <td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>
		                <td>".$jabatan."</td>
		                <td><div align=center>".$eselon."</div></td>
		                <td>".YMD2DMY(trimTime($row1->tmt), TGL_SEPARATOR)."</td>
		                <td><div align=right>".$row1->gaji_pokok."</div></td>
		                <td><div align=center>".$pejabat."</div></td>
		                <td>".$row1->no_sk."</td>
		                <td><div align=center>".YMD2DMY(trimTime($row1->tgl_sk), TGL_SEPARATOR)."</div></td>";
		                if ($ro==false) {
		                    $s .= "
		                    <td><div align='center'> <a href='".base_url()."index.php/jabatan/form_edit_struktural/".$row1->id."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."images/edit.gif' border='0'></a></div></td> ";
                            //if($this->input->post('page'))
		                    $s .="<td><div align='center'><a href=\"javascript:deleteRow(".$row1->id.")\"><img src='".base_url()."images/del.gif' border='0'></a></div></td>";
		                }
		                $s .= "
		              </tr>";
		
		                $i++;
                	} //foreach
                }

            }
            $s .= "</tbody></table>";
            return $s;
        }

        function list_jabatan_fungsional($nip="",$where="", $order="", $ro=false) {
               
        	if ($nip=="") {
        		return "";
        	} else {
                $this->db->where(array('nip' => $nip));
                $this->db->or_where(array('nip_baru' => $nip));
        	}
            $this->db->from('pegawai');
            $q = $this->db->get();

            $s = "
              <table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tbl\">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Pengalaman Bekerja</th>
                    <th>Jenis Jabatan</th>
                    <th>Gol/Ruang Pangkat</th>
                    <th>TMT</th>
                    <th>Gaji Pokok</th>
                    <th>Pejabat</th>
                    <th>Nomor SK</th>
                    <th>Tanggal SK</th>
                    ";
            if ($ro==false) {
                $s .= " <th colspan='2'> Aksi </th>";
            }
            $s .= "</tr></thead><tbody>";
			$i = 1;

            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }
                
                //data history jabatan
                $this->db->select('pengangkatan_jabatan.idspg_pengangkatan_jabatan as id, '.
                                  'pengangkatan_jabatan.id_jabatan as id_jabatan, '.
                				  'pengangkatan_jabatan.id_golongan as id_golongan, '.
                				  'pengangkatan_jabatan.gaji_pokok as gaji_pokok, '.
                				  'pengangkatan_jabatan.tmt as tmt, pengangkatan_jabatan.no_sk as no_sk, '.
                                  'pengangkatan_jabatan.tgl_sk as tgl_sk, pengangkatan_jabatan.id_pejabat as id_pejabat, '.
                                  'pengangkatan_jabatan.id_eselon as id_eselon');
                $this->db->from('pengangkatan_jabatan');
                $this->db->join('jabatan', 'jabatan.idspg_jabatan=pengangkatan_jabatan.id_jabatan');
                $this->db->where(array('pengangkatan_jabatan.id_pegawai' => $row->idspg_pegawai));
                $this->db->where(array('jabatan.tipe != ' => TIPE_JABATAN_STRUKTURAL));
                $this->db->order_by('pengangkatan_jabatan.tmt asc');
                $q1 = $this->db->get();
                if ($q1->num_rows() > 0) {
                	foreach ($q1->result() as $row1) {
		                $pejabat = Datum_model::getData('nama', 'pejabat', array('idspg_pejabat' => $row1->id_pejabat));
		                $jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $row1->id_jabatan));
		                $tipe_jabatan = Datum_model::getData('tipe', 'jabatan', array('idspg_jabatan' => $row1->id_jabatan));
		                $this->db->select('nama, pangkat');
		                $this->db->where(array('idspg_golongan' => $row1->id_golongan));
						$query = $this->db->get('golongan');
						if ($query->num_rows() > 0) {
							$r = $query->row();
							$golongan = $r->nama.' - '.$r->pangkat;
						} else {
							$golongan = '-';
						}
				                
		                $s .= "
		              <tr class=\"$kelas\" id=\"row-".$row1->id."\" >
		                <td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>
		                <td>".$jabatan."</td>
		                <td>".getTipeJabatan( $tipe_jabatan )."</td>
		                <td>".$golongan."</td>
		                <td>".YMD2DMY(trimTime($row1->tmt), TGL_SEPARATOR)."</td>
		                <td><div align=center>".$pejabat."</div></td>
		                <td>".$row1->no_sk."</td>
		                <td><div align=center>".YMD2DMY(trimTime($row1->tgl_sk), TGL_SEPARATOR)."</div></td>";
		                if ($ro==false) {
		                    $s .= "
		                    <td><div align='center'> <a href='".base_url()."index.php/jabatan/form_edit_fungsional/".$row1->id."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."images/edit.gif' border='0'></a></div></td>";                         //if($this->input->post('page'))
		                    $s .= "<td><div align='center'><a href=\"javascript:deleteRow(".$row1->id.")\"><img src='".base_url()."images/del.gif' border='0'></a></div></td>";
		                }
		                $s .= "
		              </tr>";
		
		                $i++;
                	} //foreach
                } else {
                	continue;
                }

            }
            $s .= "</tbody></table>";
            return $s;
        }

        
        function entry_jabatan_struktural($form_id, $form_action, $arr=array()) {
        	//format tmt & tgl_sk harus YMD
        	if (isset($arr['tmt'])) {
        		$a = explode(TGL_SEPARATOR, trimTime($arr['tmt']));
	        	$d_tmt = $a[2] * 1;
	        	$m_tmt = $a[1] * 1;
	        	$y_tmt = $a[0];
        	} else {
	        	$d_tmt = date("j");
	        	$m_tmt = date("n");
	        	$y_tmt = date("Y");
        	}
        	if (isset($arr['tgl_sk'])) {
        		$a = explode(TGL_SEPARATOR, trimTime($arr['tgl_sk']));
	        	$d_tgl_sk = $a[2] * 1;
	        	$m_tgl_sk = $a[1] * 1;
	        	$y_tgl_sk = $a[0];
        	} else {
	        	$d_tgl_sk = date("j");
	        	$m_tgl_sk = date("n");
	        	$y_tgl_sk = date("Y");
        	}
        	
        	$ret = "
			<form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\">
			<fieldset >
			<table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
			  <tr>
			    <td class=\"kolom\" valign=\"top\">NIP</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" name=\"nip\" id=\"nip\" value=\"".(isset($arr['nip'])?$arr['nip']:$this->session->userdata('nip'))."\" /></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Jabatan</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" size=\"38\" name=\"jabatan\" id=\"jabatan\" value=\"".(isset($arr['jabatan'])?$arr['jabatan']:"")."\" /></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Eselon</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">".combo_eselon('id_eselon', (isset($arr['id_eselon'])?$arr['id_eselon']:""), '')."</td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Gaji Pokok</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" name=\"gaji_pokok\" size=\"38\" id=\"gaji_pokok\" value=\"".(isset($arr['gaji_pokok'])?$arr['gaji_pokok']:"0")."\" /></td>
			  </tr>
			  <tr>
              	<td class='fieldname' valign=\"top\">TMT</td>
		        <td valign=\"top\">:</td>
		        <td>".combo_tanggal('d_tmt',$d_tmt,'')."-".
        	          combo_bulan('m_tmt',$m_tmt,'').
        	          "-<input name='y_tmt' id='y_tmt' type='text' size='6' value='$y_tmt'/>
          			  <a href=\"#\" id=\"date-pick\">Pilih Tanggal</a>       
        		</td>
    		  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Pejabat</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" name=\"pejabat\" id=\"pejabat\" size=\"38\" value=\"".(isset($arr['pejabat'])?$arr['pejabat']:"")."\" /></td>
			  </tr>
    		  <tr>
			    <td class=\"kolom\" valign=\"top\">Nomor SK</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" id=\"no_sk\" name=\"no_sk\" size=\"38\" value=\"".(isset($arr['no_sk'])?$arr['no_sk']:"")."\" /></td>
			  </tr>
			  <tr>
              	<td class='fieldname' valign=\"top\">Tanggal SK</td>
		        <td valign=\"top\">:</td>
		        <td>".combo_tanggal('d_tgl_sk',$d_tgl_sk,'')."-".
        	          combo_bulan('m_tgl_sk',$m_tgl_sk,'').
        	          "-<input name='y_tgl_sk' id='y_tgl_sk' type='text' size='6' value='$y_tgl_sk'/>
          			  <a href=\"#\" id=\"date-pick\">Pilih Tanggal</a>       
        		</td>
    		  </tr>
			  <tr>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">
			      <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
			      <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
			    </td>
			  </tr>
			</table>
			</fieldset>
			</form>
			  ";
        	
        	return $ret;
        }
        

        function entry_jabatan_fungsional($form_id, $form_action, $arr=array()) {
        	//format tmt & tgl_sk harus YMD
        	if (isset($arr['tmt'])) {
        		$a = explode(TGL_SEPARATOR, trimTime($arr['tmt']));
	        	$d_tmt = $a[2] * 1;
	        	$m_tmt = $a[1] * 1;
	        	$y_tmt = $a[0];
        	} else {
	        	$d_tmt = date("j");
	        	$m_tmt = date("n");
	        	$y_tmt = date("Y");
        	}
        	if (isset($arr['tgl_sk'])) {
        		$a = explode(TGL_SEPARATOR, trimTime($arr['tgl_sk']));
	        	$d_tgl_sk = $a[2] * 1;
	        	$m_tgl_sk = $a[1] * 1;
	        	$y_tgl_sk = $a[0];
        	} else {
	        	$d_tgl_sk = date("j");
	        	$m_tgl_sk = date("n");
	        	$y_tgl_sk = date("Y");
        	}
        	
        	$ret = "
			<form id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\">
			<fieldset >
			<table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
			  <tr>
			    <td class=\"kolom\" valign=\"top\">NIP</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" name=\"nip\" id=\"nip\" value=\"".(isset($arr['nip'])?$arr['nip']:"")."\" /></td>
			  </tr>
			  <tr>
			    <td valign=\"top\">Jenis Jabatan</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">
			      <input type=\"radio\" name=\"tipe\" id=\"tipe\" value=\"".TIPE_JABATAN_FUNGSIONAL_AK."\" ".(isset($arr['tipe'])?(($arr['tipe']==TIPE_JABATAN_FUNGSIONAL_AK)?"checked":""):"checked")." />
			      ".getTipeJabatan(TIPE_JABATAN_FUNGSIONAL_AK)." 
			      <input type=\"radio\" name=\"tipe\" id=\"tipe\" value=\"".TIPE_JABATAN_FUNGSIONAL_NON_AK."\" ".(isset($arr['tipe'])?(($arr['tipe']==TIPE_JABATAN_FUNGSIONAL_NON_AK)?"checked":""):"")." />
			      ".getTipeJabatan(TIPE_JABATAN_FUNGSIONAL_NON_AK)."</td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Jabatan</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" size=\"38\" name=\"jabatan\" id=\"jabatan\" value=\"".(isset($arr['jabatan'])?$arr['jabatan']:"")."\" /></td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Gol/Ruang Pangkat</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\">".combo_golongan('id_golongan', (isset($arr['id_golongan'])?$arr['id_golongan']:""), '')."</td>
			  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Gaji Pokok</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" size=\"38\" name=\"gaji_pokok\" id=\"gaji_pokok\" value=\"".(isset($arr['gaji_pokok'])?$arr['gaji_pokok']:"0")."\" /></td>
			  </tr>
			  <tr>
              	<td class='fieldname' valign=\"top\">TMT</td>
		        <td valign=\"top\">:</td>
		        <td>".combo_tanggal('d_tmt',$d_tmt,'')."-".
        	          combo_bulan('m_tmt',$m_tmt,'').
        	          "-<input name='y_tmt' id='y_tmt' type='text' size='6' value='$y_tmt'/>
          			  <a href=\"#\" id=\"date-pick\">Pilih Tanggal</a>       
        		</td>
    		  </tr>
			  <tr>
			    <td class=\"kolom\" valign=\"top\">Pejabat</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" size=\"38\" name=\"pejabat\" id=\"pejabat\" value=\"".(isset($arr['pejabat'])?$arr['pejabat']:"")."\" /></td>
			  </tr>
    		  <tr>
			    <td class=\"kolom\" valign=\"top\">Nomor SK</td>
			    <td valign=\"top\">:</td>
			    <td valign=\"top\"><input type=\"text\" id=\"no_sk\" size=\"38\" name=\"no_sk\" value=\"".(isset($arr['no_sk'])?$arr['no_sk']:"")."\" /></td>
			  </tr>
			  <tr>
              	<td class='fieldname' valign=\"top\">Tanggal SK</td>
		        <td valign=\"top\">:</td>
		        <td>".combo_tanggal('d_tgl_sk',$d_tgl_sk,'')."-".
        	          combo_bulan('m_tgl_sk',$m_tgl_sk,'').
        	          "-<input name='y_tgl_sk' id='y_tgl_sk' type='text' size='6' value='$y_tgl_sk'/>
          			  <a href=\"#\" id=\"date-pick\">Pilih Tanggal</a>       
        		</td>
    		  </tr>
			  <tr>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">&nbsp;</td>
			    <td valign=\"top\">
			      <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
			      <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
			    </td>
			  </tr>
			</table>
			</fieldset>
			</form>
			  ";
        	
        	return $ret;
        }

        function simpan($arr, $is_update=false, $id='') {
            if (!$is_update) {
                $this->db->insert('pengangkatan_jabatan', $arr);
            } else {
                $this->db->where('idspg_pengangkatan_jabatan', $id);
                $this->db->update('pengangkatan_jabatan', $arr);
            }
        }
        
        function historiJabatan($id,$jabatan){
            $this->db->from('pegawai');
            $ret=array();
            //data history jabatan
                $this->db->select('pengangkatan_jabatan.idspg_pengangkatan_jabatan as id, '.
                                  'pengangkatan_jabatan.id_jabatan as id_jabatan, '.
                                  'pengangkatan_jabatan.id_golongan as id_golongan, '.
                                  'pengangkatan_jabatan.gaji_pokok as gaji_pokok, '.
                                  'pengangkatan_jabatan.tmt as tmt, pengangkatan_jabatan.no_sk as no_sk, '.
                                  'pengangkatan_jabatan.tgl_sk as tgl_sk, pengangkatan_jabatan.id_pejabat as id_pejabat, '.
                                  'pengangkatan_jabatan.id_eselon as id_eselon');
                $this->db->from('pengangkatan_jabatan');
                $this->db->join('jabatan', 'jabatan.idspg_jabatan=pengangkatan_jabatan.id_jabatan');
                $this->db->where(array('pengangkatan_jabatan.id_pegawai' => $id));
                $this->db->where(array('jabatan.tipe' => $jabatan));
                $this->db->order_by('pengangkatan_jabatan.tmt asc');
                $q1 = $this->db->get();
                $i=0;
                if ($q1->num_rows() > 0) {
                    foreach ($q1->result() as $row1) {
                        $pejabat = Datum_model::getData('nama', 'pejabat', array('idspg_pejabat' => $row1->id_pejabat));
                        $jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $row1->id_jabatan));
                        $eselon  = Datum_model::getData('nama', 'eselon', array('idspg_eselon' => $row1->id_eselon));
                        
                        $ret[$i]['jabatan'] = $jabatan;
                        $ret[$i]['eselon'] = $eselon;
                        $ret[$i]['tmt'] = YMD2DMY(trimTime($row1->tmt), TGL_SEPARATOR);
                        $ret[$i]['gaji_pokok'] = $row1->gaji_pokok;
                        $ret[$i]['pejabat'] = $pejabat;
                        $ret[$i]['no_sk'] = $row1->no_sk;
                        $ret[$i]['no_sk'] =  YMD2DMY(trimTime($row1->tgl_sk), TGL_SEPARATOR);
                        
        
                        $i++;
                    } //foreach
        
                }
        }
        
	}
?>